﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DanhmucThietbi;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;

namespace DanhmucThietbi
{
    public partial class frmPLoai : BasicForm.parent2
    {
        PhanLoaiInfo ObjPLoai = new PhanLoaiInfo();
        CtrPhanLoai Ctr = new CtrPhanLoai();

        enum ActionMode
        { 
            NEW=0,
            EDIT=1,
            DEL=2
        }
        ActionMode AcMode = ActionMode.NEW;

        public frmPLoai()
        {
            CreateWaitDialog();
            InitializeComponent();
            SetWaitDialogCaption("Đang tải dữ liệu");
            InitGrid();
            Init();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ClearForm();
            txtMaPLoai.Properties.ReadOnly = false;
            txtMaPLoai.Focus();
            AcMode = ActionMode.NEW;
        }

        public void ClearForm()
        {
            txtMaPLoai.Text = string.Empty;
            txtTenPLoai.Text = string.Empty;
            txtMaPLoai.Focus();
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ObjPLoai.MA_PLOAI = txtMaPLoai.Text;
            ObjPLoai.TEN_PLOAI = txtTenPLoai.Text;
            if (AcMode == ActionMode.NEW) //them mới
            {
                try
                {
                    Ctr.InsertItem(ObjPLoai);
                    this.gridControl1.DataSource = Ctr.GetAll();
                    if (MessageBox.Show("Bạn muốn thêm tiếp không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ClearForm();
                        txtMaPLoai.Focus();
                        ObjPLoai = new PhanLoaiInfo();
                    }
                    else
                    {
                        txtMaPLoai.Properties.ReadOnly = true;
                        AcMode = ActionMode.EDIT;
                    }
                }
                catch (Exception ex)
                {
                    txtMaPLoai.Focus();
                    MessageBox.Show(ex.Message);
                    
                    return;
                    
                }
            }
            else
            {
                if (AcMode == ActionMode.EDIT) //chỉnh sửa
                {
                    try
                    {
                        Ctr.UpdateItem(ObjPLoai);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else //xóa
                {
                    try
                    {
                        Ctr.DeleteItem(ObjPLoai);
                        this.gridControl1.DataSource = Ctr.GetAll();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        public void Init()
        {
            //kiểm tra nếu là textbox thì cho phép di chuyển tới control ke tiep bang phim enter.
            foreach (Control item in panelControl1.Controls)
            {
                if (item.GetType() == typeof(TextEdit))
                {
                    ((TextEdit)item).EnterMoveNextControl = true;
                }
            }
            txtMaPLoai.Focus();
            txtMaPLoai.Properties.CharacterCasing = CharacterCasing.Upper;
        }

        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[2];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Phân Loại";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "MA_PLOAI";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên Loại";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TEN_PLOAI";

            //GridColumn gridColumn2 = new GridColumn();
            this.gridView1.Columns.AddRange(Cols);
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.IndicatorWidth = 30;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            this.gridControl1.DataSource = Ctr.GetAll();
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            PhanLoaiInfo ObjPLoai = (PhanLoaiInfo)gridView1.GetRow(e.RowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaPLoai.Text = ObjPLoai.MA_PLOAI;
            txtMaPLoai.Properties.ReadOnly = true;
            txtTenPLoai.Text = ObjPLoai.TEN_PLOAI;
            AcMode = ActionMode.EDIT;
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            PhanLoaiInfo ObjPLoai = (PhanLoaiInfo)gridView1.GetRow(e.FocusedRowHandle);
            //ObjNhacc = Ctr.GetByID(SelectRow["MA_NHACC"].ToString());

            //fill du lieu len form
            txtMaPLoai.Text = ObjPLoai.MA_PLOAI;
            txtMaPLoai.Properties.ReadOnly = true;
            txtTenPLoai.Text = ObjPLoai.TEN_PLOAI;
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            txtMaPLoai.Properties.ReadOnly = true;
            txtTenPLoai.Focus();
            AcMode = ActionMode.EDIT;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                AcMode = ActionMode.DEL;
                simpleButton4.PerformClick();
            }
        }

        private void txtTenPLoai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) //nhấn enter ở contrl nhập liệu cuối cùng
            {
                simpleButton4.PerformClick();
            }
        }
    }
}
